package xdTool

import java.sql.Clob

class XdWidgetSelect {

    String label //用于查询条件标签说明
    String name //控件名称
    String radio = "true"  //单选：xm-select-radio=""
    String  maxCount="0" //多选上限
    Clob options;//自定义option ：非远程搜索时生效
    String skin = "default" //皮肤： xm-select-skin="default" (default;primary;normal;warm;danger)
    String disabled  //禁用:disabled="disabled"
    String verify //必选 required
    String quotesStr;
    String splitStr ; //默认多选用“，”号分割
    //String endStr ;
    String width;
    //远程搜索配置：
    String serverFlag    // 开启远程搜索：xm-select-search="" 本地搜索；xm-select-search="url" 搜索远程
    Long dataSourceId //数据源
    Clob dataSql //数据集:sql
    String labelField; //展示值字段
    String valueField; //实际值字段
    String allFlag = "true" //是否全量展示

    String events ; //值依赖:widgetName1;widgetName1 级联查询
    Clob html //控件html代码

    Long xdWidgetId

    String creator ;//操作人
    String updator ;//操作人
    Date dateCreated;
    Date lastUpdated;

    static constraints = {
        name(unique: true,nullable: false)
        label(nullable: true)
        radio(nullable: true)
        maxCount(nullable: true)

        options(nullable: true)
        skin(nullable: true)
        width(nullable: true)
        disabled(nullable: true)
        verify(nullable: true)
        quotesStr(nullable: true)
        splitStr(nullable: true)
        //endStr(nullable: true)
        serverFlag(nullable: true)
        dataSourceId(nullable: true)
        dataSql(nullable: true)
        labelField(nullable: true)
        valueField(nullable: true)
        allFlag(nullable: true)
        events(nullable: true)

        html(nullable: true)

        xdWidgetId(nullable: false)
        creator(nullable: false)
        updator(nullable: true)
    }
    static mapping = {
        version false
    }

    /**
     * 格式化参数值
     * @return
     */
    public String formatValue(String valueStr){
        StringBuffer result = new StringBuffer("")
        if(this.radio == "true"){
            if(this.quotesStr){
                result.append("${this.quotesStr}${valueStr}${this.quotesStr}")
            }else{
                result.append(valueStr)
            }
        }else{
            def array = valueStr.split(this.splitStr)
            array?.eachWithIndex {item,index ->
                if(index!=array.length-1){
                    result.append("${this.quotesStr}${item}${this.quotesStr}${this.splitStr}")
                }else{
                    result.append("${this.quotesStr}${item}${this.quotesStr}")
                }
            }
        }
        return result.toString()
    }

    /**
     * 更新params中select值
     * @param panelId
     * @param params
     */
    public  static void formatSelectValue(Long panelId,Map params){
        def xdWidgetPanelParam = XdWidgetPanelParam.findAllByPanelIdAndClassFlag(panelId,"xd-select")
        xdWidgetPanelParam.each {
            XdWidgetSelect xdWidgetSelect = XdWidgetSelect.get(it.paramId)
            if(xdWidgetSelect && params."${xdWidgetSelect.name}" ){
                params."${xdWidgetSelect.name}" = xdWidgetSelect.formatValue(params."${xdWidgetSelect.name}")
            }
        }
    }

}
